package ru.compft.web.filters;

import ru.compft.web.CountingServletResponse;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * User: ASUS
 * Date: 23.08.2011
 * Time: 23:58:29
 * <p/>
 * Фильтр, подсчитывающий время генерации страницы
 * todo не корректно работает с JSF
 */
public class CountingFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        final HttpServletResponse httpres = (HttpServletResponse) response;
        final CountingServletResponse counter = new CountingServletResponse(httpres);

        final HttpServletRequest httpreq = (HttpServletRequest) request;
        httpreq.setAttribute("counter", counter);
        chain.doFilter(request, counter);
        counter.flushBuffer(); // Push the last bits containing HTML comment.
    }

    @Override
    public void destroy() {
        //To change body of implemented methods use File | Settings | File Templates.
    }
}
